<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_product\Entity\ShusyProductStandard;
use Drupal\shusy_product_coupon\Entity\ShusyProductCoupon;

/**
 * @OA\Schema(
 *   schema="Standard",
 *   type="object",
 *   description="Standard"
 * )
 */
class Standard extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_product_standard';
  }

  protected function setValue() {
    if ($this->entity instanceof ShusyProductStandard) {
      $value = [
        /**
         * @OA\Property(
         *   property="id",
         *   type="integer",
         *   description="ID",
         * ),
         */
        'id' => (int) $this->entity->id,
        /**
         * @OA\Property(
         *   property="sku",
         *   type="string",
         *   description="SKU",
         * ),
         */
        'sku' => (string) $this->entity->sku,
        /**
         * @OA\Property(
         *   property="standard",
         *   type="string",
         *   description="规格",
         * ),
         */
        'standard' => (string) $this->entity->standard,
        /**
         * @OA\Property(
         *   property="amount",
         *   type="number",
         *   description="价格 计算折扣后的价格",
         * ),
         */
        'amount' => $this->convertYuan($this->getPrice($this->entity)),
        /**
         * @OA\Property(
         *   property="cost",
         *   type="number",
         *   description="原价",
         * ),
         */
        'cost' => $this->convertYuan($this->entity->cost),
        /**
         * @OA\Property(
         *   property="stock",
         *   type="integer",
         *   description="库存",
         * ),
         */
        'stock' => (int) $this->getStock($this->entity),
        /**
         * @OA\Property(
         *   property="sale",
         *   type="integer",
         *   description="销量",
         * ),
         */
        'sale' => (int) $this->getSale($this->entity),
        /**
         * @OA\Property(
         *   property="purchase",
         *   type="boolean",
         *   description="是否是可进货商品",
         * ),
         */
        'purchase' => FALSE,

        'coupon' => $this->getCoupon($this->entity),
      ];
      $this->merge($value);
    }
  }

  protected function getCoupon(ShusyProductStandard $standard) {
    if (module_exists('shusy_product_coupon')) {
      $t = shusy_product_coupon_load_single(FALSE, ['product_standard_id' => $standard->id]);
      if ($t instanceof ShusyProductCoupon && $t->status == 1) {
        return [
          'amount' => $this->convertYuan($t->amount),
        ];
      }
    }
    return FALSE;
  }

  protected function getSale(ShusyProductStandard $standard) {
    $wrapper = $standard->wrapper();
    return $wrapper->sale->value();
  }

  protected function getStock(ShusyProductStandard $standard) {
    $wrapper = $standard->wrapper();
    return $wrapper->stock->value();
  }

  protected function getPrice(ShusyProductStandard $standard) {
    $amount = $standard->amount;
    $standard = $this->entity;
    $product = $this->wrapper->product->value();
    $context = [$product, $standard, $amount];

    //    drupal_alter('shushengyuan_product_amount_view', $amount, $context);

    return $amount;
  }
}
